package com.example.chatchatbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.example.chatchatbackend.entity.dto.Category;

import com.example.chatchatbackend.entity.vo.response.CategoryVO;
import com.example.chatchatbackend.mapper.CategoryMapper;
import com.example.chatchatbackend.service.CategoryService;
import com.example.chatchatbackend.utils.TimeUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 丁真
 * @since 2024-10-28 05:03:47
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    private static final LocalDateTime currentTime = TimeUtil.Time_Now.getCurrentTime(); 
    
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public List<Category> getAllCategory() {
        return this.query().list();
    }

    @Override
    public CategoryVO insertCategory(CategoryVO categoryVO) {
        Category category = new Category(null,categoryVO.getName(),this.currentTime,this.currentTime);
        if(this.save(category))
            return categoryVO;
        return null;
    }

    @Override
    public List<CategoryVO> insertCategoryList(List<CategoryVO> categoryVOList) {

        List<Category> categoryList = new ArrayList<>();

        for (CategoryVO categoryVO : categoryVOList) {
            categoryList.add(new Category(null,categoryVO.getName(),currentTime,this.currentTime));
        }
        categoryMapper.insertList(categoryList);
        return categoryVOList;
    }


    @Override
    public IPage<Category> pageThree(Integer pageNum, Integer pageSize) {
        Page<Category> page = new Page<>(pageNum, pageSize);
        IPage<Category> categoryIPage = categoryMapper.selectPage(page, null);
        return categoryIPage;
    }



    @Override
    public List<CategoryVO> findByIds(List<Integer> ids) {
        List<Category> categoryList = categoryMapper.selectBatchIds(ids);
        List<CategoryVO> categoryVOList = new ArrayList<>();

        for (Category category : categoryList) {
            CategoryVO categoryVO = new CategoryVO();
            BeanUtils.copyProperties(category, categoryVO);
            categoryVOList.add(categoryVO);  // 添加到结果列表
        }

        return categoryVOList;
    }

    @Override
    public List<CategoryVO> findByTitle(String title) {

        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.likeRight(StringUtils.hasText(title), Category::getName, title);
        List<Category> categoryList = categoryMapper.selectList(wrapper);

        List<CategoryVO> categoryVOList = new ArrayList<>();

        for (Category category : categoryList) {
            CategoryVO categoryVO = new CategoryVO();
            BeanUtils.copyProperties(category, categoryVO);
            categoryVOList.add(categoryVO);  // 添加到结果列表
        }
        return categoryVOList;
    }

}
